package xyz.zhouzhousag.server.controller;
/**
 * Description:
 * <p>
 * ClassName: LoginController
 * date: 2021/9/26 18:48
 *
 * @author Yin.Liu
 * @version 1.0
 * @since JDK 1.8
 */

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.zhouzhousag.server.common.utils.JwtUtils;
import xyz.zhouzhousag.server.pojo.dto.LoginEntity;
import xyz.zhouzhousag.server.pojo.entity.BaseResponse;
import xyz.zhouzhousag.server.service.SysCaptchaService;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName LoginController
 * @Description 登录的控制器
 * @Author Yin.Liu8
 * @Date 2021/9/26 18:48
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
@Api(tags = "管理员登录控制器")
public class LoginController {

    @Autowired
    private SysCaptchaService sysCaptchaService;

    /*@GetMapping("captcha.jpg")
    @ApiOperation(value = "验证码", httpMethod = "POST")
    public void captcha(HttpServletResponse response, String uuid) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //获取图片验证码
        BufferedImage image = sysCaptchaService.getCaptcha(uuid);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }*/

    @PostMapping(value = "/login")
    @ApiOperation(value = "登录", httpMethod = "POST")
    public BaseResponse userLogin(@RequestBody LoginEntity loginEntity, ServletResponse response) {

        /*boolean captcha = sysCaptchaService.validate(loginEntity.getUuid(), loginEntity.getCaptcha());
        if(!captcha){
            return BaseResponse.fail("验证码不正确");
        }*/

        // 获取当前用户主体
        Subject subject = SecurityUtils.getSubject();
        String msg = null;
        boolean loginSuccess = false;
        // 将用户名和密码封装成 UsernamePasswordToken 对象
        UsernamePasswordToken token = new UsernamePasswordToken(loginEntity.getUserName(), loginEntity.getPassWord());
        try {
            subject.login(token);
            msg = "登录成功。";
            loginSuccess = true;
        } catch (UnknownAccountException uae) { // 账号不存在
            msg = "用户名与密码不匹配，请检查后重新输入！";
        } catch (IncorrectCredentialsException ice) { // 账号与密码不匹配
            msg = "用户名与密码不匹配，请检查后重新输入！";
        } catch (LockedAccountException lae) { // 账号已被锁定
            msg = "该账户已被锁定，如需解锁请联系管理员！";
        } catch (AuthenticationException ae) { // 其他身份验证异常
            msg = "登录异常，请联系管理员！";
        }
        if (loginSuccess) {
            // 若登录成功，签发 JWT token
            String jwtToken = JwtUtils.sign(loginEntity.getUserName(), JwtUtils.SECRET);
            // 将签发的 JWT token 设置到 HttpServletResponse 的 Header 中
            ((HttpServletResponse) response).setHeader(JwtUtils.AUTH_HEADER, jwtToken);
            return BaseResponse.success(msg);
        } else {
            return BaseResponse.fail(msg);
        }

    }

    @GetMapping("/logout")
    @ApiOperation(value = "登出", httpMethod = "POST")
    public BaseResponse logout() {
        BaseResponse<Object> ret = new BaseResponse<>();
        return BaseResponse.success("退出登录");
    }
}
